/*
 * MIT License
 *
 * Copyright (c) 2019 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.commons.core;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * Return java.util.Date, java.sql.Date and so on, Just like LocalDate.now()
 *
 * @author Mr.css on 2017/8/21
 * @version 2020/7/24 调整return String的函数，改用Java8的API
 */
public class Now {
    private Now() {
    }

    /**
     * 获取当前时间
     *
     * @return java.util.Date
     */
    public static java.util.Date utilDate() {
        return new java.util.Date();
    }

    /**
     * 获取当前时间
     *
     * @return java.sql.Date
     */
    public static java.sql.Date sqlDate() {
        return new java.sql.Date(System.currentTimeMillis());
    }

    /**
     * 获取当前时间戳
     *
     * @return java.sql.Timestamp
     */
    public static java.sql.Timestamp sqlTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 获取当前毫秒数
     *
     * @return java.sql.Timestamp
     */
    public static long longTime() {
        return System.currentTimeMillis();
    }

    /**
     * 获取当前日期 year
     * <p>
     * E.G.: 2024
     *
     * @return date
     */
    public static String year() {
        return String.valueOf(LocalDate.now().getYear());
    }

    /**
     * 获取当前日期 month
     * <p>
     * E.G.: 2024-01
     *
     * @return date
     */
    public static String month() {
        return LocalDateUtils.MONTH.format(LocalDate.now());
    }

    /**
     * 获取当前日期 date
     * <p>
     * E.G.: 2024-01-01
     *
     * @return date
     */
    public static String date() {
        return LocalDateUtils.DATE.format(LocalDate.now());
    }

    /**
     * 获取当前日期 hour
     * <p>
     * E.G.: 2024-01-01
     *
     * @return date
     */
    public static String hour() {
        return LocalDateUtils.HOUR.format(LocalDateTime.now());
    }

    /**
     * 获取当前日期 minute
     * <p>
     * E.G.: 2024-01-01 10:11
     *
     * @return date
     */
    public static String minute() {
        return LocalDateUtils.MINUTE.format(LocalDateTime.now());
    }

    /**
     * 获取当前日期 time
     * <p>
     * E.G.: 00:00:00
     *
     * @return time
     */
    public static String time() {
        return LocalDateUtils.TIME.format(LocalTime.now());
    }

    /**
     * 获取当前日期 datetime
     * <p>
     * E.G.: 2024-01-01 00:00:00
     *
     * @return date-time
     */
    public static String dateTime() {
        return LocalDateUtils.DATETIME.format(LocalDateTime.now());
    }

    /**
     * 获取当前日期 date
     * <p>
     * E.G.: 20240109
     *
     * @return date
     */
    public static String compactDate() {
        return LocalDateUtils.COMPACT_DATE.format(LocalDate.now());
    }

    /**
     * 获取当前日期 time
     * <p>
     * E.G.: 010203
     *
     * @return time
     */
    public static String compactTime() {
        return LocalDateUtils.COMPACT_TIME.format(LocalTime.now());
    }

    /**
     * 获取当前日期 datetime
     * <p>
     * E.G.: 20240109010203
     *
     * @return date-time
     */
    public static String compactDateTime() {
        return LocalDateUtils.COMPACT_DATETIME.format(LocalDateTime.now());
    }


    /**
     * 格式化当前时间
     *
     * @param format 格式化格式
     * @return 格式化之后的时间
     */
    public static String format(String format) {
        return DateUtils.format(new Date(), format);
    }

    /**
     * 格式化当前时间
     *
     * @param format 格式化函数
     * @return 格式化之后的时间
     */
    public static String format(DateTimeFormatter format) {
        return format.format(LocalDateTime.now());
    }

    /**
     * 今天时间加上一个值，比如加1天：Now.add(Calendar.DATE, 1)
     */
    public static Calendar add(int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(field, amount);
        return calendar;
    }

    /**
     * 获取今天的季节
     *
     * @return 季节(0春 / 1夏 / 2秋 / 3冬)
     */
    public int getSeason() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH) / 3;
    }

    /**
     * 今天的日期加上n天
     *
     * @param amount 天数
     * @return Calendar
     */
    public static Calendar addDay(int amount) {
        return add(Calendar.DATE, amount);
    }
}
